package com.runchinaup.smartbelt.view.pop;

import android.content.Context;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.runchinaup.smartbelt.R;

import java.util.List;

/**
 * Created by liber on 2018/1/29.
 */

public abstract class RecyclerViewAdapter extends SecondaryListAdapter<RecyclerViewAdapter.GroupItemViewHolder, RecyclerViewAdapter.SubItemViewHolder> {


    private Context context;

    private List<DataTree<String, String>> dts;

    public RecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void setData(List datas) {
        dts = datas;
        notifyNewData(dts);
    }

    @Override
    public ViewHolder groupItemViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pop_group, null);
        return new GroupItemViewHolder(v);
    }

    @Override
    public ViewHolder subItemViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pop_sub, null);
        return new SubItemViewHolder(v);
    }

    @Override
    public void onGroupItemBindViewHolder(ViewHolder holder, int groupItemIndex) {
        Log.e("groupText" +
                "", dts.get(groupItemIndex).getGroupItem());
        ((GroupItemViewHolder) holder).tvGroup.setText(dts.get(groupItemIndex).getGroupItem());

    }

    @Override
    public void onSubItemBindViewHolder(ViewHolder holder, int groupItemIndex, int subItemIndex) {
        ((SubItemViewHolder) holder).tvSub.setText(dts.get(groupItemIndex).getSubItems().get(subItemIndex));
        ((SubItemViewHolder) holder).tvSub.setTextColor(0xff333333);
    }

    @Override
    public void onGroupItemClick(Boolean isExpand, GroupItemViewHolder holder, int groupItemIndex) {
        if (!isExpand) {
            holder.viewGroup.setVisibility(View.VISIBLE);
            holder.packDown.setImageResource(R.mipmap.down);
        } else {
            holder.viewGroup.setVisibility(View.INVISIBLE);
            holder.packDown.setImageResource(R.mipmap.pack);
        }
    }

    @Override
    public void onSubItemClick(SubItemViewHolder holder, int groupItemIndex, int subItemIndex) {
        for (int i = 0; i < dts.get(groupItemIndex).getSubItems().size(); i++) {
            String subtext = dts.get(groupItemIndex).getSubItems().get(i);
//            if (text.equals(subtext)) {
//                holder.tvSub.setTextColor(0xffFD0489);
//            } else {
//                holder.tvSub.setTextColor(0xff333333);
//            }
        }
        OnSubItemClick(dts.get(groupItemIndex).getSubItems().get(subItemIndex));
    }


    //点击对应项目的回调
    public abstract void OnSubItemClick(String tvContent);

    public static class GroupItemViewHolder extends ViewHolder {
        private TextView tvGroup;
        private View viewGroup;
        private ImageView packDown;

        public GroupItemViewHolder(View itemView) {
            super(itemView);
            tvGroup = itemView.findViewById(R.id.group_tv);
            viewGroup = itemView.findViewById(R.id.viewGroup);
            packDown = itemView.findViewById(R.id.pack_down);
        }
    }

    public static class SubItemViewHolder extends ViewHolder {
        private TextView tvSub;

        public SubItemViewHolder(View itemView) {
            super(itemView);
            tvSub = itemView.findViewById(R.id.sub_tv);
        }
    }

}
